/**
 * Created by ㅋㅋElsa on 2017/8/16.
 */
layui.define(['form', 'layedit', 'laydate','jquery','upload'], function(exports){
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer,
        laydate = layui.laydate,
        upload = layui.upload;

    form.render();

    form.on('select(regionfilter)', function(data){
        if(data.value == '其他'){
            layer.prompt({
                formType: 0,
                value:'',
                maxlength: 10,
                title: '请输入其他航行区域'
            }, function(value, index, elem){
                console.log(value);
                $('select[name="region"]').append("<option value='"+value+"'>"+value+"</option>");
                $('select[name="region"]').val(value);
                layer.close(index);
                $('select[name="region"] option').each(function() {
                    var val = $(this).val();
                    if($("select[name='region'] option[value='" + val + "']").length > 1 ){
                        $("select[name='region'] option[value='" + val + "']:gt(0)").remove();
                    }
                });
                form.render();
            });  
        }
    });

    form.on('select(insptypefilter)', function(data){
        if(data.value == '其他'){
            layer.prompt({
                formType: 0,
                value:'',
                maxlength: 10,
                title: '请输入其他检验类型'
            }, function(value, index, elem){
                console.log(value);
                $('select[name="insptype"]').append("<option value='"+value+"'>"+value+"</option>");
                $('select[name="insptype"]').val(value);
                layer.close(index);
                $('select[name="insptype"] option').each(function() {
                    var val = $(this).val();
                    if($("select[name='insptype'] option[value='" + val + "']").length > 1 ){
                        $("select[name='insptype'] option[value='" + val + "']:gt(0)").remove();
                    }
                });
                form.render();
            });  
        }
    });

    form.on('select(ccfilter)', function(data){
        if(data.value!=""){
            var c = data.value.split("/");
            var username=c[1];
            $.ajax({
                type:"GET",
                url:contextPath+"/user/ccinfo/"+username,
                contentType: "application/json; charset=utf-8",
                dataType:"json",
                success:function (data) {
                    $('input[name="apunitname"]').removeAttr("value");
                    $('input[name="ccinfo"]').removeAttr("value");
                    $('input[name="ccphone"]').removeAttr("value");
                    $('input[name="fax"]').removeAttr("value");
                    $('input[name="zipcode"]').removeAttr("value");
                    $('input[name="crecode"]').removeAttr("value");
                    $('input[name="ccadr"]').removeAttr("value");
                    $('input[name="writename"]').removeAttr("value");
                    if(data.crecode&&data.ccadr){
                        $('input[name="apunitname"]').attr("value",data.coname);
                        $('input[name="ccinfo"]').attr("value",data.name);
                        $('input[name="ccphone"]').attr("value",data.username);
                        $('input[name="fax"]').attr("value",data.fax);
                        $('input[name="zipcode"]').attr("value",data.zipcode);
                        $('input[name="crecode"]').attr("value",data.crecode);
                        $('input[name="ccadr"]').attr("value",data.ccadr);
                        $('input[name="writename"]').attr("value",data.writename);
                    }
                    form.render();
                },
                error:function (e) {
                }
            });
        }
    });

    //自定义验证规则
    form.verify({
        title: function(value) {
            if(value.length < 5) {
                return '标题至少得5个字符啊';
            }
        },
        pass: [/(.+){6,12}$/, '密码必须6到12位'],
        content: function(value) {
            layedit.sync(editIndex);
        }
    });


    exports('ConstNewItems', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});